﻿///----------------------------------------------------------------
/// Copyright (C) 2008-2010 yyssoft
/// 版权所有。
///
/// 文件名：TB_TEAM_FILEBLL.cs
/// 文件功能描述：TB_TEAM_FILE业务逻辑类
///
///
/// 创建标识：yyssoft 2014/3/18
///
/// 修改标识：
/// 修改描述：
///
/// 修改标识：
/// 修改描述：
///---------------------------------------------------------------
using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using XMK.Tadong.Model;
using XMK.Tadong.Data;
using System.Configuration;

namespace XMK.Tadong.Biz
{
    /// <summary>
    /// TB_TEAM_FILE的业务逻辑类
    /// </summary>
    public class TB_TEAM_FILEBiz
    {

        #region  私有变量

        private TB_TEAM_FILEDAL dal = new TB_TEAM_FILEDAL(Config.TadongDbProvider);

        #endregion  私有变量

        #region  构造函数

        public TB_TEAM_FILEBiz()
        { }

        #endregion  构造函数

        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回主标识ID,失败返回"0"</returns>
        public bool Add(TB_TEAM_FILEInfo model)
        {
            try
            {
                return dal.AddBySql(model);
            }
            catch (Exception e) {
                return false;
            }
           
        }

        /// <summary>
        /// 判断同一用户，文件夹下是否已有同名文件
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool HasFileName(string User_ID, string Team_ID, string File_Name, string Folder_ID)
        {
            return dal.HasFileName(User_ID, Team_ID, File_Name, Folder_ID);
        }

        /// <summary>
        /// 判断同一目录下是否有多个引用
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        public bool HasSavePath(string Save_Path)
        {
            return dal.HasSavePath(Save_Path);
        }

        public bool CopyFile(TB_TEAM_FILEInfo fileObj)
        {
            return dal.AddBySql(fileObj);
        }

         /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool Update(TB_TEAM_FILEInfo model)
        {
            return dal.UpdateBySql(model);
        }

        /// <summary>
        /// 审核数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpdateDatas(string File_ID, string Folder_ID, string Upd_Time)
        {
            return dal.UpdateDatasBySql(File_ID, Folder_ID, Upd_Time);
        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool Update(TB_TEAM_FILEInfo model,string Flag)
        {
            return dal.UpdateBySql(model, Flag);
        }
        //TEAM_ID FILE_NAME FILE_EXT FILE_ABT 
        public TB_TEAM_FILEInfo Query(string team_id, string file_name, string save_path)
        {
            DataTable dt= dal.getTeamFileyFileNameAndPath(team_id, file_name, save_path);
            if (dt.Rows.Count > 0)
            {
                TB_TEAM_FILEInfo fileInifo = new TB_TEAM_FILEInfo();
                DataRow dr = dt.Rows[0];

                 fileInifo.FILE_ID= Convert.ToInt32(dr["FILE_ID"]);
                 fileInifo.TEAM_ID = Convert.ToInt32(dr["TEAM_ID"]);
                 fileInifo.FILE_NAME=Convert.ToString( dr["FILE_NAME"]);
                 fileInifo.FILE_EXT= Convert.ToString( dr["FILE_EXT"]);
                 fileInifo.FILE_ABST= Convert.ToString(dr["FILE_ABST"]);
                 fileInifo.FILE_AUTHOR= Convert.ToString(dr["FILE_AUTHOR"]);
                 fileInifo.KEYW= Convert.ToString(dr["KEYW"]);
                 fileInifo.FILE_CRT_TIME=Convert.ToDateTime( dr["FILE_CRT_TIME"]);
                 fileInifo.FILE_VERSION=Convert.ToDecimal( dr["FILE_VERSION"]);
                 fileInifo.DEST_OBJ = Convert.ToString(dr["DEST_OBJ"]);
                 fileInifo.BIZ_OBJ = Convert.ToString(dr["BIZ_OBJ"]);
                 fileInifo.SECRET_LVL= Convert.ToString(dr["SECRET_LVL"]);
                 fileInifo.LANG_TYPE= Convert.ToDecimal(dr["LANG_TYPE"]);
                 fileInifo.FILE_TYPE= Convert.ToString(dr["FILE_TYPE"]);
                 fileInifo.SAVE_PATH= Convert.ToString(dr["SAVE_PATH"]);
                 fileInifo.USER_ID= Convert.ToString(dr["USER_ID"]);
                 fileInifo.STATUS=Convert.ToInt32( dr["STATUS"]);
                 fileInifo.CREATE_TIME = Convert.ToDateTime( dr["CREATE_TIME"]);
                 fileInifo.UPD_TIME = Convert.ToDateTime(dr["UPD_TIME"]);
                 fileInifo.SINGLERECEIPT =Convert.ToString( dr["SINGLERECEIPT"]);
                 fileInifo.PDF_PATH = Convert.ToString(dr["PDF_PATH"]);
                 fileInifo.SWF_PATH =Convert.ToString( dr["SWF_PATH"]);
                 fileInifo.FILE_SIZE = Convert.ToString(dr["FILE_SIZE"]);

                 return fileInifo;
              
            }
            return null;

        }

     

        /// <summary>
        /// 退回数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool BackFile(string SINGLERECEIPT, string FILE_ID)
        {
            return dal.BackFileBySql(SINGLERECEIPT, FILE_ID);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="File_ID">文件编号</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool Delete(string File_ID)
        {
            return dal.DeleteBySql(File_ID);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModel(string FileID)
        {
            return dal.GetModelBySql(FileID);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetNewModelBySql(string FileID)
        {
            return dal.GetNewModelBySql(FileID);
        }

        public int GetIdByPathName(string team_id, string save_path, string file_name)
        {
            return dal.GetIdByPathName(team_id, save_path, file_name);
        }
        /// <summary>
        /// 获取未审核文件
        /// </summary>
        /// <returns>文件列表</returns>
        public DataTable GetUnauditedModelBySql(string Team_ID)
        {
            return null;//dal.GetUnauditedModelBySql(Team_ID, 0, 0, ref 0);
        }

        /// <summary>
        /// 获取已审核文件
        /// </summary>
        /// <returns>文件列表</returns>
        public DataTable GetAuditedData(string Team_ID)
        {
            return null;//dal.GetAuditedDataBySql(Team_ID);
        }

         /// <summary>
        /// 审核数据
        /// </summary>
        /// <param name="FileID">文件编号</param>
        /// <returns>成功返回true，失败返回false</returns>
        public bool AuditData(string FileID, string newPath)
        {
            return dal.AuditDataBySql(FileID, newPath);
        }

        /// <summary>
        /// 审核数据
        /// </summary>
        /// <param name="FileID">文件编号</param>
        /// <returns>成功返回true，失败返回false</returns>
        public bool AuditData(string FileID)
        {
            return dal.AuditDataBySql(FileID);
        }

        /// <summary>
        /// 批量审核数据
        /// </summary>
        /// <param name="FileID">文件编号</param>
        /// <returns>成功返回true，失败返回false</returns>
        public bool AuditDatas(string FileID)
        {
            return dal.AuditDatasBySql(FileID);
        }


        /// <summary>
        /// 获取我提交的文件
        /// </summary>
        /// <returns>文件列表</returns>
        public DataTable GetMySubmitModel(string User_ID, string sorttype, string sortdesc,string status, int startNum, int endNum, ref int total)
        {
            return dal.GetMySubmitModelBySql(User_ID,sorttype,sortdesc,status,startNum, endNum, ref total);
        }


        /// <summary>
        /// 根据课题组ID获得当前课题组未审核文件
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getTeamFileInfoByTeamName(string teamName, string sorttype, string sortdesc, int startNum, int endNum, ref int total)
        {
            //跟据课题组名找到课题组id
            Tb_TeamBiz teambiz = new Tb_TeamBiz();
            //string teamId = teambiz.GetTeamIdbyName(teamName);
            //获取未审核数据
            DataTable sharedt = new DataTable();
            TB_TEAM_FILEDAL fileData = new TB_TEAM_FILEDAL(Config.TadongDbProvider);
            sharedt = fileData.GetUnauditedModelBySql(teamName, sorttype, sortdesc, startNum, endNum, ref total);

            return sharedt;

            //获取提交者名称

           // return dal.getTeamFileInfoByTeamName(teamName);

        }

        public XMKFileExtInfo GetModelByFileId(string fileId)
        {
            return dal.GetModelByFileId(fileId);
        }


        /// <summary>
        /// 根据课题组ID获得当前课题组已审核文件
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getAuditedDataByTeamName(string teamName, string sorttype, string sortdesc, int startNum, int endNum, ref int total)
        {
            //跟据课题组名找到课题组id
            Tb_TeamBiz teambiz = new Tb_TeamBiz();
            //string teamId = teambiz.GetTeamIdbyName(teamName);
            //获取已审核数据
            DataTable sharedt = new DataTable();
            TB_TEAM_FILEDAL fileData = new TB_TEAM_FILEDAL(Config.TadongDbProvider);
            sharedt = fileData.GetAuditedDataBySql(teamName, sorttype, sortdesc,startNum, endNum, ref total);
            //获取文件名称和提交者
            //DataTable binddt = new DataTable();
            //Base_RM_UserInfoBiz userbiz = new Base_RM_UserInfoBiz();
            //binddt.Columns.Add("FILE_ID", typeof(string));
            //binddt.Columns.Add("FILE_NAME", typeof(string));
            //binddt.Columns.Add("USER_NAME", typeof(string));
            //binddt.Columns.Add("CREATE_TIME", typeof(DateTime));
            //binddt.Columns.Add("UPD_TIME", typeof(DateTime));
            //for (int i = 0; i < sharedt.Rows.Count; i++)
            //{
            //    DataRow row = binddt.NewRow();
            //    DataTable dt = fileData.GetModelBySql((sharedt.Rows[i][0]).ToString());
            //    if (dt.Rows.Count == 0)
            //    {
            //        continue;
            //    }
            //    else
            //    {
            //        row[0] = dt.Rows[0]["FILE_ID"].ToString();
            //        row[1] = dt.Rows[0]["FILE_NAME"].ToString();
            //        row[2] = dt.Rows[0]["user_name"].ToString();
            //        row[3] = dt.Rows[0]["CREATE_TIME"].ToString();
            //        //row[4] = dt.Rows[0]["UPD_TIME"].ToString();
            //        binddt.Rows.Add(row);
            //    }
            //}

            return sharedt;

            //获取提交者名称

        }
        /// <summary>
        /// 课题组向项目库共享文件
        /// </summary>
        /// <param name="paths">文件路径</param>
        /// <param name="name">文件名</param>
        /// <returns></returns>
        public bool TeamShareFile(string teamid,string paths,string name)
        {


            //根据文件名称、文件路径获得当前课题组文件
             string DiskPath = ConfigurationManager.AppSettings["DiskPath"];
             paths = paths.Replace(DiskPath, "");
             paths = paths.Replace("/"+name, "");
            DataTable dt = dal.getTeamFileyFileNameAndPath(teamid,name, paths);
            //添加信息到TB_TEAM_FILE_SHARE表
            TB_TEAM_FILE_SHAREInfo sharefile = new TB_TEAM_FILE_SHAREInfo();
            sharefile.SHARE_OBJ_ID = 0;
            sharefile.SHARE_OBJ_TYPE = "20";
            sharefile.FILE_ID = (decimal)dt.Rows[0]["FILE_ID"];
            sharefile.CREATE_TIME = DateTime.Now;
            sharefile.UPD_TIME = DateTime.Now;
            bool config = dal.ShareFileToXmk(sharefile);

            return true;
            

        }

        /// <summary>
        /// 根据文件名称、文件路径获得当前课题组文件
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public DataTable getTeamFileyFileNameAndPath(string teamid,string FileName, string Path)
        {
            return dal.getTeamFileyFileNameAndPath(teamid, FileName, Path);
        }


        /// <summary>
        /// 重新提交
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool AgainDataSubmit(TB_TEAM_FILEInfo model, string Flag)
        {
            return dal.AgainDataSubmitBySql(model, "1");
        }

         /// <summary>
        /// 重新提交
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool NewAgainDataSubmit(string File_ID)
        {
            return dal.NewAgainDataSubmitBySql(File_ID);
        }

         /// <summary>
        /// 修改源数据
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateSourceData(TB_TEAM_FILEInfo model)
        {
            return dal.UpDateSourceDataBySql(model);
        }

        /// <summary>
        /// 修改关键词和摘要信息
        /// </summary>
        /// <param name="model">实体对象</param>
        /// <returns>成功返回true,失败返回false</returns>
        public bool UpDateInfo(TB_TEAM_FILEInfo model, string types)
        {
            return dal.UpDateInfoBySql(model, types);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public int GetCountByDestObj(string folder_id)
        {
            return dal.GetCountByDestObj(folder_id);
        }

        /// <summary>
        /// 获取文件总数
        /// </summary>
        /// <param name="folder_id">文件夹ID</param>
        /// <param name="keyword">关键词</param>
        /// <returns></returns>
        public int GetCountByDestObjByKeyWord(string folder_id, string keyword)
        {
            return dal.GetCountByDestObjByKeyWord(folder_id, keyword);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObjAndType(string op, string folder_id, int startNum, int endNum, string TEAM_ID, string USER_ID, ref int total)
        {
            return dal.GetModelByDestObjAndType(op,folder_id, startNum, endNum, TEAM_ID, USER_ID, ref total);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObj(string folder_id, int startNum, int endNum,string TEAM_ID,string USER_ID, ref int total)
        {
            return dal.GetModelByDestObj(folder_id, startNum, endNum,TEAM_ID,USER_ID, ref total);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="DestObjId">文件所在目标ID</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelByDestObjAndKeyWord(string folder_id, int startNum, int endNum, string TEAM_ID, string USER_ID,string keyword, ref int total)
        {
            return dal.GetModelByDestObjAndKeyWord(folder_id, startNum, endNum, TEAM_ID, USER_ID, keyword, ref total);
        }
        

        /// <summary>
        /// 根据文件ID获得文件
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public TB_TEAM_FILEInfo GetTeamFile(string File_ID)
        {
            DataTable dt = dal.GetTeamFileBySql(File_ID);
            if (dt.Rows.Count > 0)
            {
                TB_TEAM_FILEInfo fileInifo = new TB_TEAM_FILEInfo();
                DataRow dr = dt.Rows[0];

                fileInifo.FILE_ID = Convert.ToInt32(dr["FILE_ID"]);
                fileInifo.TEAM_ID = Convert.ToInt32(dr["TEAM_ID"]);
                fileInifo.FILE_NAME = Convert.ToString(dr["FILE_NAME"]);
                fileInifo.FILE_EXT = Convert.ToString(dr["FILE_EXT"]);
                fileInifo.FILE_ABST = Convert.ToString(dr["FILE_ABST"]);
                fileInifo.FILE_AUTHOR = Convert.ToString(dr["FILE_AUTHOR"]);
                fileInifo.KEYW = Convert.ToString(dr["KEYW"]);
                fileInifo.FILE_CRT_TIME = Convert.ToDateTime(dr["FILE_CRT_TIME"]);
                fileInifo.FILE_VERSION = Convert.ToDecimal(dr["FILE_VERSION"]);
                fileInifo.DEST_OBJ = Convert.ToString(dr["DEST_OBJ"]);
                fileInifo.BIZ_OBJ = Convert.ToString(dr["BIZ_OBJ"]);
                fileInifo.SECRET_LVL = Convert.ToString(dr["SECRET_LVL"]);
                fileInifo.LANG_TYPE = Convert.ToDecimal(dr["LANG_TYPE"]);
                fileInifo.FILE_TYPE = Convert.ToString(dr["FILE_TYPE"]);
                fileInifo.SAVE_PATH = Convert.ToString(dr["SAVE_PATH"]);
                fileInifo.USER_ID = Convert.ToString(dr["USER_ID"]);
                fileInifo.STATUS = Convert.ToInt32(dr["STATUS"]);
                fileInifo.CREATE_TIME = Convert.ToDateTime(dr["CREATE_TIME"]);
                fileInifo.UPD_TIME = Convert.ToDateTime(dr["UPD_TIME"]);
                fileInifo.SINGLERECEIPT = Convert.ToString(dr["SINGLERECEIPT"]);
                fileInifo.PDF_PATH = Convert.ToString(dr["PDF_PATH"]);
                fileInifo.SWF_PATH = Convert.ToString(dr["SWF_PATH"]);
                fileInifo.FILE_SIZE = Convert.ToString(dr["FILE_SIZE"]);
                fileInifo.FOLDER_ID = Convert.ToString(dr["FOLDER_ID"]);
                return fileInifo;

            }
            return null;
        }

       

        //数据库是否有文件
        public bool IsMoreThanOne(string File_ID)
        {
            return dal.IsMoreThanOne(File_ID);

        }

        //根据文件ID将文件移动到回收站
        public bool RemoveToRecycle(string File_ID)
        {
            return dal.RemoveToRecycleBySql(File_ID);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="FileID">文件IDs</param>
        /// <returns>文件列表</returns>
        public DataTable GetModelList(string FileID)
        {
            return dal.GetModelListBySql(FileID);
        }

         /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileId">ids</param>
        /// <returns></returns>
        public bool DeleteIN(string fileId)
        {
            return dal.DeleteIN(fileId);
        }

        //根据文件ID将文件从回收站移回空间
        public bool RecycleToSpace(string File_ID)
        {
            return dal.RecycleToSpaceBySql(File_ID);
        }
        //根据文件名获取当前空间同名文件的最大版本号
        public decimal GetMaxFileTeam(string fileName, string Team_ID)
        {
            return dal.GetMaxFileTeam(fileName, Team_ID);

        }
        /// <summary>
        /// 获取回收站信息
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>结果集</returns>
        public DataTable GetRecycleDataBySql(string teamid, string sorttype, string sortdesc, int startNum, int endNum, ref int total)
        {
            return dal.GetRecycleDataBySql(teamid, sorttype, sortdesc,startNum, endNum, ref total);
        }

        #endregion  成员方法

    }
}

